clear all
set more off



********************************************************************************
* Tables 1
* Last updated: 20 December 2020
********************************************************************************
**Loading dataset
use "${data_prep}/dat_fiveweeks.dta", clear

/*Figure 1a Treatment Effects on Beliefs Restaruants*/

preserve 
	*** Separate Regressions
	sort phone week_number
	xtset phone week_number
	bysort phone: gen week_in_sample = _n
	keep if week_in_sample == 1 

	/* Defining biased beliefs */
	gen bias_prior_rest = prior_plan_rest - district_rest
	gen bias_prior_park = prior_plan_park - district_park
	*Standardized beliefs (beliefs/std(beliefs district))
	bysort district_index: egen sd_rest = sd(bias_prior_rest)
	bysort district_index: egen sd_park = sd(bias_prior_park)
	gen bias_r_st = bias_prior_rest/sd_rest
	gen bias_p_st = bias_prior_park/sd_park 
	**Interaction terms with treatment
	gen demand_br = demand*bias_r_st
	gen demand_br_br = demand*bias_r_st*bias_r_st
	gen demand_bp = demand*bias_p_st
	gen demand_bp_bp = demand*bias_p_st*bias_p_st
	*Labels vars
	label var demand_br "Treatment * Bias"
	label var demand_br_br "Treatment * Bias{superscript:2}"
	label var demand_bp "Treatment * Bias"
	label var demand_bp_bp "Treatment * Bias{superscript:2}"

	** Regular Controls
	local controls_strata "rest_frequent_pre i.district_index supply"
	local controls_extra "education_low  `controls_strata' i.week_number"
	*Plot changes in treatment effects over distribution of bias
	local treatment_d2_r "demand_br demand_br_br" 
	eststo int1r:  reg post_actual_rest  demand##c.bias_r_st##c.bias_r_st  `controls_extra' , r
	eststo int1rm:  margins, dydx(demand) at(bias_r_st=(-2(0.1)2)) post 
		
	coefplot (int1rm,ciopts(recast(rline) lpattern(dash) color(orange_red)) color(orange_red)) , title(" ", margin(small)) ///
    at recast(line) lwidth(*2) xscale(lstyle(none)) yline(0) lpattern(dot) ytitle("Treatment Effects on Beliefs" "% Neighbors Visiting Restaurants") xscale(lstyle(none) titlegap(0.5))  ///
	ylabel(, format(%03.1f) nogrid) fcolor(blue) xlabel(-2 "-2{&sigma}" -1 "-{&sigma}" 0 `""Truth" " ""' 1 "+{&sigma}" 2 "+2{&sigma}") saving(belief_rest_inter.gph,replace) 
	graph export "${graphs}rev_app_figure_ate_rest.pdf", replace
	
	local treatment_d2_r "demand##high_prior" 
	
	eststo e_1:  reg post_actual_rest `treatment_d2_r' `controls_extra', r

	margins, dydx(demand) over(high_prior) post 
	coefplot, yline(0) vertical recast(bar) barwidth(0.40) finten(0)  /// 
	citop ciopt(recast(rcap) color(maroon)) color(orange_red*.9) xlabel(1  `" "Below" "Truth"' 2 `" "Above" "Truth"')  fxsize(30)	///
	saving(belief_ate_rest, replace)  title(" ", margin(small))  xscale(lstyle(none) titlegap(0.5)) ylabel(,nolabel nogrid)
	
	*** Saving Figure paper main text
	graph combine belief_rest_inter.gph belief_ate_rest.gph, imargin(0 0 0 0) graphregion(margin(l=22 r=22)) cols(2) ycommon graphregion(margin(small)) b1(Prior Belief Relative to True Percentage) 
	graph export "${graphs}rev_figure_ate_beliefs_rest.pdf", replace
	
restore

/*Figure 1B Treatment Effects on Behavior Restaruants*/

preserve 
	*** Separate Regressions
	sort phone week_number
	xtset phone week_number
	bysort phone: gen week_in_sample = _n
	keep if week_in_sample == 1 

	/* Defining biased beliefs */
	gen bias_prior_rest = prior_plan_rest - district_rest
	gen bias_prior_park = prior_plan_park - district_park
	*Standardized beliefs (beliefs/std(beliefs district))
	bysort district_index: egen sd_rest = sd(bias_prior_rest)
	bysort district_index: egen sd_park = sd(bias_prior_park)
	gen bias_r_st = bias_prior_rest/sd_rest
	gen bias_p_st = bias_prior_park/sd_park 
	**Interaction terms with treatment
	gen demand_br = demand*bias_r_st
	gen demand_br_br = demand*bias_r_st*bias_r_st
	gen demand_bp = demand*bias_p_st
	gen demand_bp_bp = demand*bias_p_st*bias_p_st
	*Labels vars
	label var demand_br "Treatment * Bias"
	label var demand_br_br "Treatment * Bias{superscript:2}"
	label var demand_bp "Treatment * Bias"
	label var demand_bp_bp "Treatment * Bias{superscript:2}"

	***Restaurants
	** Regular Controls
	local controls_strata "rest_frequent_pre i.district_index supply"
	local controls_extra "education_low  `controls_strata'  plan_index_rest i.week_number"
	
	*Plot changes in treatment effects over distribution of bias
	local treatment_d2_r "demand_br demand_br_br" 
	eststo int1r:  reg rest_actual_index demand##c.bias_r_st##c.bias_r_st  `controls_extra' , r
	eststo int1rm:  margins, dydx(demand) at(bias_r_st=(-2(0.1)2)) post 
		
	coefplot (int1rm,ciopts(recast(rline) lpattern(dash) color(orange_red)) color(orange_red)) , ///
    at recast(line) lwidth(*2) xscale(lstyle(none)) yline(0) ytitle("Treatment Effects on Restaurants Visits") ///
	ylabel(, format(%03.1f) nogrid) fcolor(blue) xlabel(-2 "-2{&sigma}" -1 "-{&sigma}" 0 `""Truth" " ""' 1 "+{&sigma}" 2 "+2{&sigma}") saving(behavior_rest_inter,replace)
	
		**ATE
		local treatment_d2_r "demand##high_prior i.week_number supply " 
		eststo e_1:  reg rest_actual_index `treatment_d2_r' `controls_extra' `ccontrols_r', r
		margins, dydx(demand) over(high_prior) post 

	
	coefplot, yline(0) vertical recast(bar)  color(orange_red*.9)  barwidth(0.40) finten(30) /// 
	citop ciopt(recast(rcap) color(maroon))  yscale(r(-0.4 0.4))  xlabel(1  `" "Below" "Truth"' 2 `" "Above" "Truth"')   fxsize(30)	///
	saving(behavior_ate_rest, replace)    xscale(lstyle(none)) ylabel(,nolabel nogrid) 

	*** Saving Figure paper main text
	
	graph combine behavior_rest_inter.gph behavior_ate_rest.gph, imargin(0 0 0 0) graphregion(margin(l=22 r=22)) cols(2) ycommon graphregion(margin(small)) b1(Prior Belief Relative to True Percentage) 
	graph export "${graphs}rev_main_behavior.pdf", replace

	restore
	
	
	/*Figure 3A: Moderator Analysis Risk Aversion */
	
	preserve 
	*** Separate Regressions
	sort phone week_number
	xtset phone week_number
	bysort phone: gen week_in_sample = _n
	keep if week_in_sample == 1 
	keep if high_prior==0
	local controls_strata "rest_frequent_pre i.district_index supply"
	local controls_extra "education_low  `controls_strata'  plan_index_rest i.week_number"
	

	sum pref_risk_health, detail
	gen risk_dummy=0 if pref_risk_health!=.
	replace risk_dummy=1 if pref_risk_health>`r(p50)'
	sum pref_risk_general, detail
	gen all_risk_dummy=0 if pref_risk_general!=.
	replace all_risk_dummy=1 if pref_risk_general>`r(p50)'

	*Plot changes in treatment effects over distribution of bias
	
	local treatment_d2_r "demand##all_risk_dummy" 
	eststo int1r: qui reg rest_actual_index `treatment_d2_r'  `controls_extra' , r
	eststo risk_gl:  margins, dydx(demand) at(all_risk_dummy==0) post 
	eststo int1r: qui reg rest_actual_index `treatment_d2_r'  `controls_extra' , r

	eststo risk_gh:  margins, dydx(demand) at(all_risk_dummy==1) post 

	
	local treatment_d2_r "demand##risk_dummy" 
	eststo int1r: qui reg rest_actual_index `treatment_d2_r'  `controls_extra' , r
	eststo risk_hl:  margins, dydx(demand) at(risk_dummy==0) post 
	eststo int1r: qui reg rest_actual_index `treatment_d2_r'  `controls_extra' , r
	
	eststo risk_hh:  margins, dydx(demand) at(risk_dummy==1) post 
	
		
	coefplot (risk_gl, bcolor(gold*0.5) lwidth(1.12) ciopt(recast(rcap) lwidth(1.12) color(maroon)) ) ///
	(risk_hl, color(navy*0.8) lwidth(1.12) ciopt(recast(rcap) lwidth(1.12) color(blue)))  ///
	(risk_gh, color(gold*0.5) lwidth(1.12) ciopt(recast(rcap) lwidth(1.12) color(maroon))) ///
	(risk_hh,  color(navy*0.8) lwidth(1.12) ciopt(recast(rcap) lwidth(1.12) color(blue))) , ///
	yline(0) vertical recast(bar) citop barwidth(0.15) finten(0) fcolor(darkblue*.5) ylabel(, format(%03.1f) nogrid) ///
	ytitle("Treatment Effects" "Restaurant Visits") xlabel(0.8 "Risk Averse" 1.2 "Risk Tolerant") xscale(lstyle(none)) ///
	legend(order(1 "General Risk" 3 "Health Risk") pos(top) cols(2))   grid(off)    
 	graph export "${graphs}rev_app_figure_het_risk.pdf", replace
	restore 
	
	/*FIGURE 3B: PARKS. Changes in Beliefs*/
	preserve 
	*** Separate Regressions
	sort phone week_number
	xtset phone week_number
	bysort phone: gen week_in_sample = _n
	keep if week_in_sample == 1 

	/* Defining biased beliefs */
	gen bias_prior_rest = prior_plan_rest - district_rest
	gen bias_prior_park = prior_plan_park - district_park
	*Standardized beliefs (beliefs/std(beliefs district))
	bysort district_index: egen sd_rest = sd(bias_prior_rest)
	bysort district_index: egen sd_park = sd(bias_prior_park)
	gen bias_r_st = bias_prior_rest/sd_rest
	gen bias_p_st = bias_prior_park/sd_park 
	**Interaction terms with treatment
	gen demand_br = demand*bias_r_st
	gen demand_br_br = demand*bias_r_st*bias_r_st
	gen demand_bp = demand*bias_p_st
	gen demand_bp_bp = demand*bias_p_st*bias_p_st
	*Labels vars
	label var demand_br "Treatment * Bias"
	label var demand_br_br "Treatment * Bias{superscript:2}"
	label var demand_bp "Treatment * Bias"
	label var demand_bp_bp "Treatment * Bias{superscript:2}"

		*** Margins plot 
	local controls_strata "rest_frequent_pre i.district_index supply"
	local controls_extra "education_low  `controls_strata' i.week_number"
		
	eststo e_1:  reg post_actual_park demand##high_prior_park `controls_extra', r

	margins, dydx(demand) over(high_prior_park) post 
	coefplot, yline(0) vertical recast(bar) barwidth(0.40) finten(0)  /// 
	citop ciopt(recast(rcap) color(dkgreen)) color(green*.9) xlabel(1  `" "Below" "Truth"' 2 `" "Above" "Truth"')  fxsize(30)	///
	saving(belief_ate_park, replace)   xscale(lstyle(none) titlegap(0.5)) ylabel(,nolabel nogrid)

	
	reg post_actual_park  demand##c.bias_p_st##c.bias_p_st  `controls_extra' , r
	eststo int2r: qui margins, dydx(demand) at(bias_p_st=(-2(0.1)2)) post 
		
	coefplot (int2r,ciopts(recast(rline) lpattern(dash) color(green)) color(green)) , ///
    at recast(line) lwidth(*2) lpattern(dot) xscale(lstyle(none)) yline(0) ytitle("Treatment Effects on Beliefs" "% Neighbors Visiting Parks") ///
	ylabel(, format(%03.1f) nogrid) fcolor(blue) xlabel(-2 "-2{&sigma}" -1 "-{&sigma}" 0 `""Truth" " ""' 1 "+{&sigma}" 2 "+2{&sigma}") saving(belief_parks_inter.gph,replace)
	
	graph combine belief_parks_inter.gph belief_ate_park.gph, imargin(0 0 0 0) graphregion(margin(l=22 r=22)) cols(2) ycommon graphregion(margin(small)) b1(Prior Belief Relative to True Percentage) 
	graph export "${graphs}rev_app_figure_ate_bl_parks.pdf", replace
	
	
	/*FIGURE 3B: PARKS. Changes in Beliefs*/
	preserve 
	*** Separate Regressions
	sort phone week_number
	xtset phone week_number
	bysort phone: gen week_in_sample = _n
	keep if week_in_sample == 1 

	/* Defining biased beliefs */
	gen bias_prior_rest = prior_plan_rest - district_rest
	gen bias_prior_park = prior_plan_park - district_park
	*Standardized beliefs (beliefs/std(beliefs district))
	bysort district_index: egen sd_rest = sd(bias_prior_rest)
	bysort district_index: egen sd_park = sd(bias_prior_park)
	gen bias_r_st = bias_prior_rest/sd_rest
	gen bias_p_st = bias_prior_park/sd_park 
	**Interaction terms with treatment
	gen demand_br = demand*bias_r_st
	gen demand_br_br = demand*bias_r_st*bias_r_st
	gen demand_bp = demand*bias_p_st
	gen demand_bp_bp = demand*bias_p_st*bias_p_st
	*Labels vars
	label var demand_br "Treatment * Bias"
	label var demand_br_br "Treatment * Bias{superscript:2}"
	label var demand_bp "Treatment * Bias"
	label var demand_bp_bp "Treatment * Bias{superscript:2}"

		*** Margins plot 
	local controls_strata "rest_frequent_pre i.district_index supply"
	local controls_extra "education_low  `controls_strata' i.week_number"
		
	eststo e_1:  reg post_actual_park demand##high_prior_park `controls_extra', r

	margins, dydx(demand) over(high_prior_park) post 
	coefplot, yline(0) vertical recast(bar) barwidth(0.40) finten(0)  /// 
	citop ciopt(recast(rcap) color(dkgreen)) color(green*.9) xlabel(1  `" "Below" "Truth"' 2 `" "Above" "Truth"')  fxsize(30)	///
	saving(belief_ate_park, replace)   xscale(lstyle(none) titlegap(0.5)) ylabel(,nolabel nogrid)

	
	reg post_actual_park  demand##c.bias_p_st##c.bias_p_st  `controls_extra' , r
	eststo int2r: qui margins, dydx(demand) at(bias_p_st=(-2(0.1)2)) post 
		
	coefplot (int2r,ciopts(recast(rline) lpattern(dash) color(green)) color(green)) , ///
    at recast(line) lwidth(*2) lpattern(dot) xscale(lstyle(none)) yline(0) ytitle("Treatment Effects on Beliefs" "% Neighbors Visiting Parks") ///
	ylabel(, format(%03.1f) nogrid) fcolor(blue) xlabel(-2 "-2{&sigma}" -1 "-{&sigma}" 0 `""Truth" " ""' 1 "+{&sigma}" 2 "+2{&sigma}") saving(belief_parks_inter.gph,replace)
	
	graph combine belief_parks_inter.gph belief_ate_park.gph, imargin(0 0 0 0) graphregion(margin(l=22 r=22)) cols(2) ycommon graphregion(margin(small)) b1(Prior Belief Relative to True Percentage) 
	graph export "${graphs}rev_app_figure_ate_bl_parks.pdf", replace
	
	
	
	/*FIGURE 3C: PARKS. Changes in Behavior*/

	
preserve 
	*** Separate Regressions
	sort phone week_number
	xtset phone week_number
	bysort phone: gen week_in_sample = _n
	keep if week_in_sample == 1 

	/* Defining biased beliefs */
	gen bias_prior_rest = prior_plan_rest - district_rest
	gen bias_prior_park = prior_plan_park - district_park
	*Standardized beliefs (beliefs/std(beliefs district))
	bysort district_index: egen sd_rest = sd(bias_prior_rest)
	bysort district_index: egen sd_park = sd(bias_prior_park)
	gen bias_r_st = bias_prior_rest/sd_rest
	gen bias_p_st = bias_prior_park/sd_park 
	**Interaction terms with treatment
	gen demand_br = demand*bias_r_st
	gen demand_br_br = demand*bias_r_st*bias_r_st
	gen demand_bp = demand*bias_p_st
	gen demand_bp_bp = demand*bias_p_st*bias_p_st
	*Labels vars
	label var demand_br "Treatment * Bias"
	label var demand_br_br "Treatment * Bias{superscript:2}"
	label var demand_bp "Treatment * Bias"
	label var demand_bp_bp "Treatment * Bias{superscript:2}"

	***Restaurants
	** Regular Controls
	local controls_strata "rest_frequent_pre i.district_index supply"
	local controls_extra "education_low  `controls_strata'  plan_index_park i.week_number"
	
	*Plot changes in treatment effects over distribution of bias
	local treatment_d2_r "demand_bp demand_bp_bp" 
	eststo int1r:  reg park_actual_index demand##c.bias_p_st##c.bias_p_st  `controls_extra' , r
	eststo int1rm:  margins, dydx(demand) at(bias_p_st=(-2(0.1)2)) post 
		
	coefplot (int1rm,ciopts(recast(rline) lpattern(dash) color(green)) color(green)) , ///
    at recast(line) lwidth(*2) xscale(lstyle(none)) yline(0) ytitle("Treatment Effects on Park Visits") ///
	ylabel(, format(%03.1f) nogrid) fcolor(blue) xlabel(-2 "-2{&sigma}" -1 "-{&sigma}" 0 `""Truth" " ""' 1 "+{&sigma}" 2 "+2{&sigma}") saving(behavior_park_inter,replace)
	
		**ATE
		local treatment_d2_p "demand##high_prior_park i.week_number supply " 
		eststo e_1:  reg park_actual_index `treatment_d2_p' `controls_extra' `ccontrols_p', r
		margins, dydx(demand) over(high_prior_park) post 

	
	coefplot, yline(0) vertical recast(bar)  color(green*.9)  barwidth(0.40) finten(30) /// 
	citop ciopt(recast(rcap) color(dkgreen))  yscale(r(-0.5 1))  xlabel(1  `" "Below" "Truth"' 2 `" "Above" "Truth"')   fxsize(30)	///
	saving(behavior_ate_park, replace)    xscale(lstyle(none)) ylabel(,nolabel nogrid) 

	*** Saving Figure paper main text
	
	graph combine behavior_park_inter.gph behavior_ate_park.gph, imargin(0 0 0 0) graphregion(margin(l=22 r=22)) cols(2) ycommon graphregion(margin(small)) b1(Prior Belief Relative to True Percentage) 
	graph export "${graphs}rev_main_park_behavior.pdf", replace

	restore
